﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Threading;

namespace Worker
{
    public class Program
    {
        public static void Main(string[] args)
        {
            const string queueName = "task_queue";
            Console.WriteLine("worker runing");
            ConnectionFactory factory = new ConnectionFactory();
            factory.HostName = "192.168.64.130";
            factory.UserName = "root";
            factory.Password = "root";
            factory.Port = 5672;

            using (var conn=factory.CreateConnection())
            {
                using (var channel=conn.CreateModel())
                {
                    channel.QueueDeclare(queueName, false, false, false, null);

                    var consumer = new EventingBasicConsumer(channel);

                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);

                        Console.WriteLine("received {0}", message);

                        int dots = message.Split('.').Length - 1;

                        Thread.Sleep(dots * 1000);

                        Console.WriteLine(" done ");
                    };

                    channel.BasicConsume(queueName, true, consumer);

                    Console.WriteLine("worker ending");

                    Console.ReadLine();
                }
            }
            
        }
    }
}
